#include "PressureObject.h"
#include "include.h"

#include <assert.h>
PressureObject::PressureObject(TextureManager * textureManager, Vertex position)
:TranslatingObject(textureManager, position)
{	
	path->SetMin(2);
	path->SetMax(2);
		
	CreatePropertyMenu();
	
	float frictionCheck = 100.0;
	int size = 0;
	while(frictionCheck > 1)
	{
		size++;
		frictionCheck /= 10;
	}
	size += 6;
	char * frictionString = "100.0";
	char grabbableString[2] = "1";
	char stickyString[2] = "0";
	char offRightString[2] = "0";
	propertyMenu->SetFormContent(0,"1.0");
	propertyMenu->SetFormContent(1,frictionString);
	propertyMenu->SetFormContent(2,grabbableString);
	propertyMenu->SetFormContent(3,stickyString);
	propertyMenu->SetFormContent(4,offRightString);
	propertyMenu->SetFormContent(5,"");
	propertyMenu->Hide();
}
PressureObject::PressureObject(TextureManager * textureManager, char * name, VertexList * vertices, char * textureName,
								Vector2D * textureVertices, Vector2D position, VertexList * path, float mass, 
								float speed, bool offRight, bool sticky, bool isGrabbable)
:TranslatingObject(textureManager, 0, vertices, NULL, position, NULL, mass, speed)
{
	printf("Making PressureObject\n");
	printf("Texture name = %s\n", textureName);
	assert(vertices->GetLength() == 4);
	this->path = new Object(textureManager, path);
	path->SetMin(2);
	path->SetMax(2);

	for(int i = 0; i < 4; i++)
		this->textureVertices[i] = textureVertices[i];
		
	this->material = new Material("", textureName, 1.0, speed, isGrabbable, 1.0);
	SetMaterial(this->material);	
	
	CreatePropertyMenu();
	
	float frictionCheck = speed;
	int size = 0;
	while(frictionCheck > 1)
	{
		size++;
		frictionCheck /= 10;
	}
	size += 6;
	char * springString = (char*)malloc(sizeof(char)*size);
	sprintf(springString,"%.3f",speed);
	char grabbableString[2] = {isGrabbable?'1':'0','\0'};
	char stickyString[2] = {sticky?'1':'0','\0'};
	char offRightString[2] = {offRight?'1':'0','\0'};
	propertyMenu->SetFormContent(1,springString);
	propertyMenu->SetFormContent(2,grabbableString);
	propertyMenu->SetFormContent(3,stickyString);
	propertyMenu->SetFormContent(4,offRightString);
	propertyMenu->SetFormContent(5,name);
	propertyMenu->Hide();
	printf("Done making pressure object\n");
}
PressureObject::~PressureObject()
{
}
char * PressureObject::GetTag()
{
	printf("Getting pressureObject tag\n");
	
	char * points = vertices->GetRelativeTag(position.GetPosition());
	char * materialName = material->name;
	Vector2D right = path->GetVertices()->head->payload->GetPosition();
	Vector2D left = path->GetVertices()->head->next->payload->GetPosition();
	
	Vector2D axis = !(right - left);
	float rightDist = position.GetPosition().dist(right);
	float leftDist = position.GetPosition().dist(left);
	
	
	char * mass = propertyMenu->GetFormContent(0);
	char * springConstant = propertyMenu->GetFormContent(1);
	char * isGrabbable = propertyMenu->GetFormContent(2);
	char * isSticky = propertyMenu->GetFormContent(3);
	char * offRight = propertyMenu->GetFormContent(4);
	char * name = propertyMenu->GetFormContent(5);
	VertexList * textureList = new VertexList();
	Vertex * pos = new Vertex(position);
	char * posString = pos->GetTag();
	printf("Here1\n");
	for(int i = 3; i >= 0; i--)
	{
		textureList->Add(new Vertex(textureVertices[i]));
	}
	printf("Here2\n");
	char * texturePoints = textureList->GetTag();
	printf("Here3\n");
	char * tag = (char *)malloc(strlen(points)+strlen(materialName)+strlen(mass)+strlen(springConstant)+
								strlen(isGrabbable)+strlen(posString)+strlen(isSticky)+ strlen(offRight) + strlen(name)+strlen(texturePoints)+256);
	printf("Here4\n");
	sprintf(tag, "<object,pressureobject,%s,%d, %s, %s, %s, %s,(%f, %f), (%f,%f), %s, %s, %s, %s, %s >", 
			name, vertices->GetLength(),points, posString,mass, isGrabbable,
			axis.x, axis.y, rightDist, leftDist, offRight, isSticky, springConstant, material->fileName, texturePoints);
	printf("Here5\n");
	delete pos;
	delete points;
	free(posString);
	free(mass);
	free(springConstant);
	free(isGrabbable);
	free(isSticky);
	free(offRight);
	free(name);
	textureList->ClearList();
	delete textureList;
	printf("Here6\n");
	return tag;	
	
}
void PressureObject::CreatePropertyMenu()
{
	printf("PressureObject::CreatePropertyMenu\n");
	if(propertyMenu)
	{
		delete(propertyMenu);
		propertyMenu = NULL;
	}
	propertyMenu = new FormMenu(textureManager, "6,Name,1,offRight,1,Sticky,1,isGrabbable,1,Spring,1,Mass,1", PROPERTY_MENU_POSITION, 0);
	float massCheck = mass;
	int size = 0;
	while(massCheck > 1)
	{
		size++;
		massCheck /= 10;
	}
	size += 6;
	char * massString = (char*)malloc(sizeof(char)*size);
	sprintf(massString, "%.3f", mass);
	propertyMenu->SetFormContent(0,massString);
}